<template>
  <div class="login-bg">
    <div class="login-box">
      <h1>欢 迎 登 录</h1>
      <el-form :model="loginForm" status-icon :rules="rules" ref="ruleForm" class="demo-ruleForm">
        <el-form-item label="用户名" prop="username">
          <el-input type="text" v-model="loginForm.username" placeholder="请输入用户名"></el-input>
        </el-form-item>
        <el-form-item :label="'密码'" prop="password">
          <el-input type="password" v-model="loginForm.password" placeholder="请输入密码"></el-input>
        </el-form-item>

        <el-form-item>
          <el-button class="button" :loading="loading" type="primary" @click="submitForm(loginForm)">登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import {handleLogin} from "@/api/common";

export default {
  name: 'login',
  data() {
    return {
      loading: false,
      loginForm: {
        username: '',
        password: '',
      },
      rules: {
        username: [
          {required: true, message: '请输入用户名', trigger: 'blur'},
        ],
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'},
        ],
      },
    }
  },
  methods: {
    submitForm(data) {
      // 点击加载
      this.loading = true
      // 发送请求响应
      handleLogin(data).then(response => {
        const data = response.data
        if (data.code===0){
          this.$message({
            message: data.msg,
            type: 'success'
          });
          localStorage.setItem('edb-authorization-token', data.data)
          localStorage.setItem('admin', this.loginForm.username)
          this.$router.push('/')
        }
        this.loading=false
      }).catch(error => {
        this.$message.error(error)
      })
    }
  }
}
</script>

<style scoped lang="scss">
.login-bg {
  width: 100%;
  height: 100%;
  position: fixed;
  top: 0;
  left: 0;
  min-width: 1000px;
  z-index: -10;
  zoom: 1;
  background-color: #fff;
  background-image: url(../../assets/background.png);
  background-repeat: no-repeat;
  background-size: cover;
  -webkit-background-size: cover;
  -o-background-size: cover;
  background-position: center 0;


  h1 {
    text-align: center;
    color: #000;
    padding-left: 40px;
    margin: 13px;
  }
  .login-box {
    width: 590px;
    height: 350px;
    background-color: #fff;
    border: 1px #DCDCDC solid;
    position: absolute;
    left: 50%;
    top: 40%;
    transform: translate(-50%, -50%);
    padding-top: 20px;
    padding-right: 40px;
    border-radius: 20px;
    box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);

    // 表单样式
    .demo-ruleForm {
      margin-left: 40px;
    }
    .button {
      width: 100%;
    }

  }
}
</style>